package com.sq.universe.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.extra.mail.MailUtil;
import com.alibaba.fastjson.JSON;
import com.sq.universe.base.service.impl.BaseServiceImpl;
import com.sq.universe.base.utils.AuthUtils;
import com.sq.universe.system.dao.MsgDao;
import com.sq.universe.system.entity.MsgEntity;
import com.sq.universe.system.listener.WebSocketHandler;
import com.sq.universe.system.service.MsgService;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author 孙乾 2021-08-23 22:40:35
*/
@Service
@Slf4j
public class MsgServiceImpl extends BaseServiceImpl<MsgEntity, MsgDao> implements MsgService {

    @Autowired
    private MsgDao msgDao;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    /**
     * 根据用户查询
     * @param msgEntity
     * @return
     */
    public List<MsgEntity> queryByUser(MsgEntity msgEntity){
        msgEntity.setCurrentUser(AuthUtils.getCurrentUser().getUserId());
        return msgDao.queryByUser(msgEntity);
    }

    public Integer insert(MsgEntity msgEntity){
        msgEntity.setMsgCode(DateUtil.format(new Date(),"yyyyMMddHHmmssSSS"));
        msgEntity.setMsgSender(AuthUtils.getCurrentUser().getUserId());
        String[] msgTypeArr = msgEntity.getMsgType().split(",");
        String[] receiverArr = msgEntity.getMsgReceiver().split(",");
        for(String msgType : msgTypeArr){
            if(StringUtils.isNotBlank( msgEntity.getMsgReceiver())){
                for (String receiver : receiverArr){
                    msgEntity.setMsgType(msgType);
                    msgEntity.setMsgReceiver(receiver);
                    msgEntity.setMsgId(null);
                    msgDao.insert(msgEntity);
                    rocketMQTemplate.convertAndSend("system-msg", msgEntity);
                    WebSocketHandler.userChannel.get(AuthUtils.getCurrentUser().getUserId()).writeAndFlush(new TextWebSocketFrame("[服务器在]" + LocalDateTime.now()+" + 接收到消息，消息为：" ));
                }
            }else{
                msgEntity.setMsgType(msgType);
                msgEntity.setMsgId(null);
                msgDao.insert(msgEntity);
                rocketMQTemplate.convertAndSend("system-msg", msgEntity);
            }
        }
        if(StringUtils.isNotBlank(msgEntity.getMsgMailReceiver())){
            try {
                ArrayList<String> tos = CollUtil.newArrayList(msgEntity.getMsgMailReceiver());
                MailUtil.send(tos, msgEntity.getMsgTitle(), msgEntity.getMsgContentHtml(), true);
            }catch (Exception e){
                log.error("邮件发送错误",e);
            }
        }
        return msgEntity.getMsgId();
    }

}
